﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using S.Authorization.API.Application.Command.UserCommand;
using S.Authorization.Common.Helper;
using S.Authorization.Common.RabbitMQ;
using S.Authorization.Domain.Model.Authority;
using S.Authorization.Domain.Model.User;
using S.Authorization.Domain.SqlServer;
using S.Common.Enums.Organization;

using static NPOI.HSSF.Util.HSSFColor;

namespace S.Authorization.API.Controllers.User
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly IConfiguration configuration;
        private readonly SqlDbContext db;
        public UserController(IMediator _mediator, IConfiguration _configuration,SqlDbContext _db)
        {
            this.mediator = _mediator;
            configuration = _configuration;
            db = _db;
        }
        /// <summary>
        /// 添加功能
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost("CreateUser")]
        public async Task<IActionResult> CreateUser(UserCreatelCommond command)
        {
            return Ok(await mediator.Send(command));
        }
        /// <summary>
        /// 人员增加
        /// </summary>
        /// <param name="goods"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost("UserAdd")]
        public IActionResult UserAdd(UsersModel users)
        {
            try
            {
                ///RabbitMq案例
                ///消费逻辑
                //RabbitHelper.rabbitmq.Receive(MessageDataMq.exchangeName,
                //    MessageDataMq.routingKey,
                //    MessageDataMq.queueName,
                //    (msg) =>
                //    {
                //        UsersModel goods = JsonConvert.DeserializeObject<UsersModel>(msg);
                //        var list = db.AddAsync(goods);
                //    }, MessageDataMq.exchangeType
                //    );

                RabbitHelper.rabbitmq.SendAsync(MessageDataMq.exchangeName, MessageDataMq.routingKey, MessageDataMq.queueName, users, MessageDataMq.exchangeType);

                // 打印获取到的数据
                return Ok("UserAdd operation initiated successfully.");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                throw;
            }
        }
        [HttpGet("ConsumeMessage")]
        public IActionResult ConsumeMessage()
        {
            RabbitHelper.rabbitmq.Consume("RealTimeDataQueue");
            return Ok();
        }
        /// <summary>
        /// 用户名修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut("UpdateUser")]
        public async Task<IActionResult> UpdateUser(int ID,string? Name,string? UpdateBy)
        {
            return Ok(await mediator.Send(new UserUpdateCommand() { ID=ID,Name=Name, UpdateBy = UpdateBy }));
        }
        /// <summary>
        /// 密码修改
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="PassWord"></param>
        /// <param name="UpdateBy"></param>
        /// <returns></returns>
        [HttpPut("UpdatePasswordUser")]
        public async Task<IActionResult> UpdatePasswordUser(int ID, string? PassWord, string? UpdateBy)
        {
            return Ok(await mediator.Send(new UserUpdatePassWordCommand() { ID = ID, PassWord = PassWord, UpdateBy = UpdateBy }));
        }
        /// <summary>
        /// 手机号修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut("UpdatePhoneUser")]
        public async Task<IActionResult> UpdatePhoneUser(int ID, string? Phone, string? UpdateBy)
        {
            return Ok(await mediator.Send(new UserUpdatePhoneCommand() { ID = ID, Phone = Phone, UpdateBy = UpdateBy }));
        }
        /// <summary>
        /// 直接从 Apollo 中获取配置值
        /// </summary>
        /// <param name="key">apollo中唯一的key值</param>
        /// <returns></returns>

        [HttpGet]
        public IActionResult Get()
        {
            //通过配置绑定，我们可以直接从 Apollo 中获取配置值。、
            var value = configuration.GetValue<string>("tes1");
            if (value != null)
            {
                return Ok(value);
            }
            else
            {
                return NotFound();
            }
        }

        /// <summary>
        /// 用户表删除
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<bool> DeleteUser(int userId)
        {
            return await mediator.Send(new UserDeleteCommand()
            {
                UserId = userId
            });
        }

    }
}
